package face.threaddemo;

import java.util.Random;

import demo.java8.Person;

/**
 * Created by zhangyanfei on 2019/6/26.
 * 生产者
 *
 * @author zhangyanfei
 * @date 2019/06/26
 */
public class ProducerWithSignal implements Runnable {
    private String name;
    private Storage storage;

    ProducerWithSignal(String name, Storage storage) {
        this.name = name;
        this.storage = storage;
    }

    @Override
    public void run() {
        while (true) {
            try {
                while (storage.size() >= Storage.MAX_SIZE) {
                    storage.wait();
                }
                System.out.println(name + "准备生产");
                Person person = new Person("" + (int)(Math.random() * 10000));
                storage.set(person);
                Thread.sleep(500);
                System.out.println(name + "已生产了(" + person.toString() + ").");
                System.out.println("===============");
                storage.notifyAll();

            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
